package com.jrx.anytxn.param.service.system.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.system.ChannelTableReq;
import com.jrx.anytxn.param.dto.system.ChannelTableRes;
import com.jrx.anytxn.param.entity.PrChannelTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrChannelTableMapper;
import com.jrx.anytxn.param.service.system.IChannelTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 渠道表 业务接口实现
 *
 * @author yx
 * @date 2019/5/28
 */
@Service
@CacheConfig(cacheNames = "CHANNEL")
public class ChannelTableServiceImpl implements IChannelTableService {
    private static final Logger logger = LoggerFactory.getLogger(ChannelTableServiceImpl.class);
    @Resource
    private ExtPrChannelTableMapper extPrChannelTableMapper;

    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<ChannelTableRes> findAllPageByTenId(Integer pageNum, Integer pageSize,String tenantId) throws TxnException {
        Page<PrChannelTable> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrChannelTable> channelTableList = extPrChannelTableMapper.selectAllByTenId(tenantId);
        if (channelTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<ChannelTableRes> res = BeanMapping.copyList(channelTableList, ChannelTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public ChannelTableRes addChannelTable(ChannelTableReq channelTableReq) throws TxnException {
        PrChannelTable channelTable = extPrChannelTableMapper.selectByChannelIdAndTenantId(channelTableReq.getChannelId(), channelTableReq.getTenantId());
        if (channelTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrChannelTable prChannelTable = BeanMapping.copy(channelTableReq, PrChannelTable.class);
        try {
            prChannelTable.setId(segmentService.getId(Constant.PR_CHANNEL_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        prChannelTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrChannelTableMapper.insertSelective(prChannelTable);
        return BeanMapping.copy(prChannelTable, ChannelTableRes.class);
    }

    @CacheEvict(key = "'channel_'+#channelTableReq.tenantId+'_'+#channelTableReq.channelId")
    @Override
    public ChannelTableRes modifyChannelTable(ChannelTableReq channelTableReq) throws TxnException {
        PrChannelTable channelTable = extPrChannelTableMapper.selectByChannelIdAndTenantId(channelTableReq.getChannelId(),channelTableReq.getTenantId());
        if (channelTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrChannelTable prChannelTable = BeanMapping.copy(channelTableReq, PrChannelTable.class);
        // 更新
        extPrChannelTableMapper.updateByPrimaryKeySelectiveAndNotNull(prChannelTable);
        return BeanMapping.copy(prChannelTable, ChannelTableRes.class);
    }

    @CacheEvict(key = "'channel_'+#tenantId+'_'+#channelId")
    @Override
    public Boolean removeByChannelaAndTenId(String channelId,String tenantId) throws TxnException {
        if (StringUtils.isBlank(channelId)||StringUtils.isBlank(tenantId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrChannelTableMapper.deleteByChannelIdAndTenId(channelId,tenantId);
        return i > 0;
    }

    @Override
    public ChannelTableRes findByChannelAndTenId(String channelId,String tenantId) throws TxnException {
        if (StringUtils.isBlank(channelId)||StringUtils.isBlank(tenantId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrChannelTable channelTable = extPrChannelTableMapper.selectByChannelIdAndTenantId(channelId,tenantId);
        return BeanMapping.copy(channelTable, ChannelTableRes.class);
    }

    @Cacheable(key="'channel_'+#tenantId+'_'+#channelId")
    @Override
    public PrChannelTable getChannelTableByChannelIdAndStatus(String status, String channelId,String tenantId) throws TxnBizException {
        if (StringUtils.isBlank(channelId)) {
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrChannelTable channelTable = extPrChannelTableMapper.selectByChannelIAndStatus(channelId, status,tenantId);
        if (channelTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        return channelTable;
    }

    @Override
    public List<ChannelTableRes> findChannelListByOrgId(String orgId,String tenantId) throws TxnException {
        if (StringUtils.isBlank(orgId)) {
            logger.error("额度节点机构号为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY);
        }
        List<PrChannelTable> channelTableList = extPrChannelTableMapper.selectChannelListByOrgIdAndTenId(orgId,tenantId);
        return BeanMapping.copyList(channelTableList, ChannelTableRes.class);
    }

    @Override
    public List<ChannelTableRes> findAllChannel(String tenantId) {
        List<PrChannelTable> prChannelTableList = extPrChannelTableMapper.selectAllByTenId(tenantId);
        return BeanMapping.copyList(prChannelTableList, ChannelTableRes.class);
    }


}
